<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>

    <style>
        *{
            margin: 0;
            padding: 0;
            list-style: none;
        }

        .mark {
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: rgba(0, 0, 0, .4);
        }

        .box {
            width: 175px;
            position: absolute;
            top: 240px;
            left: 100px;
            height: 200px;
            background: #fff;
        }
    </style>
</head>
<body>
    
    <!-- app 为父组件 -->
    <div id="app">
        <div>
            我的爱好为：
            <ul>
                <li v-for="item in filterList">
                    {{ item.title }}
                </li>
            </ul>
        </div>
        <button @click="isShowFlag">点我</button>
        <my-dialog @cancel="cancel" v-show="flag"></my-dialog>
    </div>


    <!-- 子组件 -->
    <template id="dialog">
        <div class="mark">
            <div class="box">
                <ul>
                    <li v-for="(item, index) in list">
                        <input v-model="item.check" type="checkbox">
                        {{ item.title }}
                    </li>
                </ul>

                <button @click="success">确定</button>
                ---------
                <button @click="changeFlag">取消</button>
            </div>
        </div>
    </template>

    <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
    <script>

        const DiaLog = {
            template: "#dialog",
            data() {
                return {
                    list: [
                        {
                            title: "篮球",
                            check: false
                        },
                        {
                            title: "足球",
                            check: false
                        },
                        {
                            title: "乒乓球球",
                            check: false
                        }
                    ]
                }
            },
            methods: {
                changeFlag() {
                    // 自定义事件， 传递内容
                    this.$emit("cancel", {
                        flag: false
                    })
                },
                // 点击确定
                success() {
                    this.$emit("cancel", {
                        flag: false,
                        list: JSON.parse(JSON.stringify(this.list))
                    })
                }
            }
        }

        new Vue({
            // 元素
            el: "#app",
            components: {
                "my-dialog": DiaLog
            },
            // 对数据重新进行计算
            computed: {
                filterList() {
                    return this.list.filter(item => item.check)
                }
            },
            data() {
                return {
                    flag: false,
                    list: []
                }
            },
            methods: {
                isShowFlag() {
                    this.flag = true
                },
                // 修改flag
                cancel(obj) {
                    this.flag = obj.flag
                    if (obj.list) {
                        this.list = obj.list
                    } 
                }
            }
        })
    </script>
</body>
</html>